package com.zzz.bayer.ui

import android.Manifest
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.text.SpannableString
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ImageUtils
import com.tbruyelle.rxpermissions2.RxPermissions
import com.zzz.bayer.BayerHelper
import com.zzz.bayer.GrayHelper
import com.zzz.bayer.R
import com.zzz.bayer.bean.BayerItem
import com.zzz.bayer.bean.BayerType
import com.zzz.bayer.bean.GrayItem
import com.zzz.bayer.bean.GrayType
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*
import org.angmarch.views.SpinnerTextFormatter


class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
    private lateinit var disposable: Disposable
    private lateinit var sourceBitmap: Bitmap

    private val grayList by lazy {
        arrayListOf(
            GrayItem(GrayType.WEIGHTING, "加权平均"),
            GrayItem(GrayType.AVG, "平均值"),
            GrayItem(GrayType.MAX, "最大值"),
            GrayItem(GrayType.MIN, "最小值"),
            GrayItem(GrayType.MAX_MIN_AVG, "最大最小平均"),
            GrayItem(GrayType.RED, "红色通道"),
            GrayItem(GrayType.GREEN, "绿色通道"),
            GrayItem(GrayType.BLUE, "蓝色通道")
        )
    }
    private val bayerList by lazy {
        arrayListOf(
            BayerItem(BayerType.EASY_DITHER, "简单阈值比较算法"),
            BayerItem(BayerType.ORDER_DITHER, "有序抖动算法"),
            BayerItem(BayerType.ERROR_DIFFUSION_FLOYD, "误差扩散算法-Floyd&Steinberg"),
            BayerItem(BayerType.DOT_DIFFUSION, "点扩散算法")
        )
    }

    private val grayTextFormatter by lazy {
        SpinnerTextFormatter<GrayItem> { item -> SpannableString(item.grayName) }
    }

    private val bayerTextFormatter by lazy {
        SpinnerTextFormatter<BayerItem> { item -> SpannableString(item.bayerName) }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        disposable = RxPermissions(this).request(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
            .subscribe {

            }
        //
        spGray.setSpinnerTextFormatter(grayTextFormatter)
        spGray.setSelectedTextFormatter(grayTextFormatter)
        spGray.attachDataSource(grayList)
        //
        spBayer.setSpinnerTextFormatter(bayerTextFormatter)
        spBayer.setSelectedTextFormatter(bayerTextFormatter)
        spBayer.attachDataSource(bayerList)
        //
        launch(Dispatchers.IO) {
            sourceBitmap = BitmapFactory.decodeStream(assets.open("lenna.jpg"))
            launch(Dispatchers.Main) {
                ivSource.setImageBitmap(sourceBitmap)
            }
        }
        btnExecute.setOnClickListener {
            launch(Dispatchers.IO) {
                //灰度转化
                val grayItem = spGray.selectedItem as GrayItem
                val grayBitmap = when (grayItem.grayType) {
                    GrayType.AVG -> GrayHelper.average(sourceBitmap)
                    GrayType.WEIGHTING -> GrayHelper.weighting(sourceBitmap)
                    GrayType.MAX -> GrayHelper.max(sourceBitmap)
                    GrayType.MIN -> GrayHelper.min(sourceBitmap)
                    GrayType.MAX_MIN_AVG -> GrayHelper.maxminAvg(sourceBitmap)
                    GrayType.RED -> GrayHelper.red(sourceBitmap)
                    GrayType.GREEN -> GrayHelper.green(sourceBitmap)
                    GrayType.BLUE -> GrayHelper.blue(sourceBitmap)
                    else -> sourceBitmap
                }
                launch(Dispatchers.Main) {
                    ivGray.setImageBitmap(grayBitmap)
                }
                //半色调转化
                val bayerItem = spBayer.selectedItem as BayerItem
                val bayerBitmap = when (bayerItem.bayerType) {
                    BayerType.EASY_DITHER -> BayerHelper.easyDither(grayBitmap)
                    BayerType.ORDER_DITHER -> BayerHelper.orderDither(grayBitmap)
                    BayerType.ERROR_DIFFUSION_FLOYD -> BayerHelper.errorDiffusionFloyd(grayBitmap)
                    BayerType.DOT_DIFFUSION -> BayerHelper.dotDiffusion(grayBitmap)
                    else -> sourceBitmap
                }
                ImageUtils.save(
                    bayerBitmap,
                    "/sdcard/Android/${bayerItem.bayerName}.png",
                    Bitmap.CompressFormat.PNG
                )
                launch(Dispatchers.Main) {
                    ivBayer.setImageBitmap(bayerBitmap)
                }
            }

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        cancel()
        if (!disposable.isDisposed) {
            disposable.dispose()
        }
    }
}
